#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>

int main()
{
  pid_t id=fork();
  if(id==0)
  {
    int cnt=3;
    while(cnt)
    {
      sleep(3);
      printf("我是一个子进程，pid：%d，ppid：%d\n",getpid(),getppid());
      sleep(1);
      cnt--;
    }
    exit(10);
  }
  while(1)
  {
    int status=0;
    pid_t rid=waitpid(id,&status,WNOHANG);
    if(rid>0)
    {
      //if(WIFEXITED(status)) 
      //rintf("wait success,rid:%d,exit code:%d,exit signal:%d\n",rid,WEXITSTATUS(status),status&0x7f);
      printf("wait success,rid:%d,exit code:%d,exit signal:%d\n",rid,(status>>8)&0xff,status&0x7f);
      break;
    }
    else if(rid==0)
    {
      printf("本轮调用结束，子进程没有退出\n");
      sleep(1);
    }
    else
    {
      printf("等待失败\n");
      sleep(1);
    }
  }
  return 0;
}
